﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace NJL.Web
{
    public partial class FindPassword : BasePageVisitor
    {
        private readonly BLL.NJL_USER bll = new BLL.NJL_USER();

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadPageSEO();
        }

        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = "找回密码-"+WEBSITE_SYS_BASE.WEBSITE_NAME;

            //HtmlMeta myMeta = new HtmlMeta();
            //myMeta.Name = "Keywords";
            //myMeta.Content = WEBSITE_SYS_BASE.WEBSITE_KEYWORDS;
            //this.Header.Controls.Add(myMeta);

            //HtmlMeta myMeta1 = new HtmlMeta();
            //myMeta1.Name = "Description";
            //myMeta1.Content = WEBSITE_SYS_BASE.WEBSITE_DESCRIPTION;
            //this.Header.Controls.Add(myMeta1);
        }

        protected void btnSubmitEmail_Click(object sender, EventArgs e)
        {
            //
            string strEmail = txtEmail.Text.Trim();
            string strVCode = txtVCode2.Text.Trim();
            if (strVCode.ToLower() != Session["CheckCodeEmail"].ToString().ToLower())
            {
                Royalsoft.Common.MessageBox.Show(this, "验证码输入有误");
                //Page.RegisterClientScriptBlock("", "<script>alert('验证码输入有误！');</script>");
                return;
            }

            Model.NJL_USER model = bll.GetModelByEmail(strEmail);

            if (model == null)
            {
                Royalsoft.Common.MessageBox.Show(this, "用户不存在");
               // Page.RegisterClientScriptBlock("", "<script>alert('用户不存在！');</script>");
                return;
            }
            else
            {
                if (model.TYPE_CODE == "4")
                {
                    Royalsoft.Common.MessageBox.Show(this, "此帐号已被禁用，无法获取密码！如有疑问请联系网站管理员。");
                    //Page.RegisterClientScriptBlock("", "<script>alert('此帐号已被禁用，无法获取密码！如有疑问请联系网站管理员。');</script>");
                    return;
                }
                else
                {
                    RegisterMail(model.EMAIL, model.USER_PASSWORD,model.EMAIL);
                    Royalsoft.Common.MessageBox.ShowAndRedirect(this, "登录密码已发送到您的邮箱，请查收！", "Login.aspx");
                    //Page.RegisterClientScriptBlock("", "<script>alert('登录密码已发送到您的邮箱，请查收！');window.location.href='Login.aspx';</script>");
                }
            }
        }
 

        private System.Net.Mail.MailMessage RegisterMail(string txtMailTo, string Code, string LoginName)
        {
            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(System.Configuration.ConfigurationManager.AppSettings["MailAddressFrom"], txtMailTo.Trim());
            mm.Subject = System.Configuration.ConfigurationManager.AppSettings["WebSiteName"]+"-密码找回";
            mm.Body = "农家乐网登录名：" + LoginName + "， 登录密码：" + Code + "。";
            mm.IsBodyHtml = true;
            smtp.Send(mm);
            return mm;
        }

    }
}